#pragma once
#include <pthread.h>
#include <iostream>
#include <string>
//本hpp作用是封装一下锁
class LockProtect
{
public:
    LockProtect(pthread_mutex_t* pmutex)
        : _pmutex(pmutex)
    {}
    ~LockProtect()
    {}
    void Lock()
    {
        pthread_mutex_lock(_pmutex);
    }
    void Unlock()
    {
        pthread_mutex_unlock(_pmutex);
    }
private:
    pthread_mutex_t* _pmutex;
};
class ProtectResourse
{
public:
    ProtectResourse(pthread_mutex_t* pmutex)
        : _lp(pmutex)
    {
        _lp.Lock();
    }
    ~ProtectResourse()
    {
        _lp.Unlock();
    }
private:
    LockProtect _lp;
};
typedef ProtectResourse PrRes;

struct LockAndName
{
    static std::string Name()
    {
        static int a = 1;
        char buffer[64];
        snprintf(buffer, sizeof(buffer), "thread-%d", a++);
        return buffer;
    }
    LockAndName(pthread_mutex_t *pmutex)
        : _pmutex(pmutex), _name(Name())
    {}
    pthread_mutex_t *_pmutex;
    std::string _name;
};